package com.suse.jd_system_server.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.suse.jd_system_server.dao.DatasourceInitDao;
import com.suse.jd_system_server.entity.HotelData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Iterator;

/**
 * 数据初始化工具类
 */
@Component
public class DataInitUtil {
    private static DatasourceInitDao dao;

    @Autowired
    public void setDao(DatasourceInitDao dao) {
        DataInitUtil.dao = dao;
    }
    /**
     * hotel表数据插入
     * 需要递归获取一个对象内的需要数值，赋给entity实体类中的对应属性，一次创建一个新对象进行数据库表的插入
     * 获取多个String依次赋给新对象进行数据库操作
     *
     * @param addr，待插入的值
     */
    public void insertHotelData(JsonNode addr) throws Exception {
        HotelData hotel;
        Iterator<JsonNode> elements = addr.get("results").elements();
        JsonNode node;

        while(elements.hasNext()){
            node = elements.next();
            hotel = new HotelData();

            hotel.setHotelName(node.get("name").asText());
            hotel.setAddx(node.get("location").get("lng").asDouble());
            hotel.setAddy(node.get("location").get("lat").asDouble());
            hotel.setProvince(node.get("province").asText());
            hotel.setCity(node.get("city").asText());
            hotel.setRegion(node.get("area").asText());
            hotel.setAddr(node.get("address").asText());
            hotel.setIsParking(node.get("detail").asInt());
            hotel.setTelphone(node.get("telephone").asText());
//            循环中单个地址详细信息
            JsonNode detail = BaiduMapUtil.getDetail(String.valueOf(node.get("uid"))).get("result");
            hotel.setHotelStar(detail.get("detail_info").get("facility_rating").asLong());
            hotel.setDescription(detail.get("detail_info").get("environment_exterior").asText());
//            其他数据
            hotel.setCountry("中国");
            hotel.setArea(Math.random()*500+200);
//            数据插入
            if(dao!=null){
                dao.hotelInsert(hotel);
            }else{
                System.out.println("dao为空");
            }
        }
    }
}
